
//页面的类型常量及变量
var LOGIN_SWITCH=7;
var START_LOAD_PIC=8;
var STORE_ONLINE=11;
var BOOK_SEARCH=12;
var BOOK_READ_LOAD=13;
var BOOK_DETAIL_LOAD=14;
var BOOK_CHAPTER_SWITCH=15;
var vCOMP=LOGIN_SWITCH

//页面数据分类
var ONEDAY=1;
var ONEMOUTH=2;
var vTimeCode=ONEDAY;

function getUSERType(){
	var thisURL = document.URL;   
	var value =thisURL.split('?')[1];  
	vCOMP= Number(value.split('=')[1]);  
	console.log("thisURL="+thisURL+",vCOMP="+vCOMP);		 
}

function onTime(e){
	var timeId= e.getAttribute("id");
	e.className="select-style";
	console.log("channelId="+timeId)
	var theOtherElement= "";
	if(timeId=="left-title"){
		vTimeCode=ONEDAY;
		theOtherElement=document.getElementById("right-title");
	}else if(timeId=="right-title"){
		vTimeCode=ONEMOUTH;
		theOtherElement=document.getElementById("left-title");
	}
	theOtherElement.className="unselect-style";
	console.log("vTimeCode="+vTimeCode)
			
	mRequest.send();
}


window.onload= function (){
    getUSERType();	
	mTitle.setTitleBar();
	//console.log(mRequest.requestUrl());
	mRequest.send();
	
	
}
/*****************数据请求及处理*******************/
var mRequest={
	requestUrl:function(){
		var url=addMethod(strUrlHost,"usertimedelay");
		url=addToken(url);
		var itemcode = vCOMP;
		url=addItemCode(url,itemcode)
		var optType=vTimeCode;
		url=addOptionType(url,optType);
	    console.log("url:"+url);
		return url;
	},
	send:function(){
			sendRequest(this.requestUrl(), window.mData.process);
			console.log("send");		
	}
	
}
/******************数据处理及图表绘制*************************/
var mData={
	process:function (resultData){	
		var flag = resultData.success;	
		console.log("flag="+flag);		
		if(flag==1){
			var vData=resultData.data;			
			var type=vCOMP;
			switch(type){
			case 7:
			case 8:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			    console.log("--------"+type+"--------");
				mData.processCOMPData(vData);
                break;								
			}
		}
	},
	processCOMPData:function(vData){
		console.log(vData.length);		
		var charTime="";
		var MIGU=0;
		var QQ=0;
		var PALM=0;
		var chartDataArray_MIGU= new Array();
		var chartDataArray_QQ= new Array();
		var chartDataArray_PALM= new Array();
		mHTML.clearRow();
		for(var i=0;i<vData.length;i++){
			var temp=vData[i];
            var timeTemp=temp.TIME;			
		    if(vTimeCode==ONEDAY){
				var timeSlice=timeTemp.slice(8,10)
				charTime=timeSlice+":00";
			}else if(vTimeCode==ONEMOUTH){
				var charTimeTemp=timeTemp.slice(4,6)+'.'+timeTemp.slice(6,8);
				var timeSlice=timeTemp.slice(6,8);
				charTime=timeTemp.slice(0,4)+'.'+charTimeTemp;
			} 
			MIGU=vData[i].MIGU;
			QQ=vData[i].QQ;
			PALM=vData[i].PALM;
			if(MIGU==null&&QQ==null&&PALM==null){
				console.log("MIGU,QQ and PALM are undefined")
				MIGU=i;
				QQ=2*i;
				PALM=3*i;
			}
			console.log("charTime="+charTime+",MIGU="+MIGU+",QQ"+QQ,",PALM"+PALM);
			mHTML.addRow(charTime,MIGU,QQ,PALM);
			var j=vData.length-1-i;
			chartDataArray_MIGU[j]=[timeSlice,MIGU];		
			chartDataArray_QQ[j]=[timeSlice,QQ];
            chartDataArray_PALM[j]=[timeSlice,PALM];			
		}
		mHTML.drawChart(chartDataArray_MIGU,chartDataArray_QQ,chartDataArray_PALM);
		mHTML.drawRow();
	}	
}
/************图表绘制**************************/
var mHTML={
	strRow:"",
	clearRow:function(){
		mHTML.strRow="";
	},
	addRow:function(charTime,MIGU,QQ,PALM){	
		mHTML.strRow+= "<tr><td>"+charTime+"</td><td>"+MIGU+"s</td><td>"+QQ+"s</td><td>"+PALM+"s</td></tr>";
		console.log(mHTML.strRow);		
	},
	drawRow:function(){
		var tbodyElement=document.getElementById("tBody");
		tbodyElement.innerHTML=mHTML.strRow;
	},
	drawChart:function(dataArray1,dataArray2,dataArray3){
		var myChart = new JSChart('chart', 'line');	
		myChart.setDataArray(dataArray1,'line1');
		myChart.setDataArray(dataArray2,'line2');
		myChart.setDataArray(dataArray3,'line3');
		myChart.setLineColor('#63a9ea', 'line1');
		myChart.setLineColor('#ff69b4', 'line2');
		myChart.setLineColor('#9d16fc', 'line3');
		myChart.setFlagColor('#63a9ea', 'line1');
		for(var i=0;i<dataArray1.length;i++){
			myChart.setTooltip([dataArray1[i][0],"咪咕:"+dataArray1[i][1],"line1"]);
			myChart.setTooltip([dataArray2[i][0],"QQ阅读:"+dataArray2[i][1],"line2"]);
			myChart.setTooltip([dataArray3[i][0],"掌阅:"+dataArray3[i][1],"line3"]);
		}
	    myChart.setTitle('');
	    myChart.setAxisNameX('');
	    myChart.setAxisNameY('');
		myChart.setAxisValuesNumberX(24);
	    myChart.setAxisPaddingTop(10);	
	    myChart.setAxisPaddingLeft(30);
	    myChart.resize(screen.width, 217);
	    myChart.setGridColor('#5D5F5D');
	    myChart.setGrid(true);
	    //myChart.setBackgroundColor('#0000ff');	
	    myChart.setShowXValues(false);
	    myChart.draw();	
	}
}

/************标题栏**************************/
var mTitle = {    
	setTitleBar:function(){		   	
			switch(window.vCOMP){
			case  LOGIN_SWITCH:
				this.setTitle("切换帐号登录");
				break;
			case  START_LOAD_PIC:		
				this.setTitle("启动-首页刷出图片");
				break;
			case  STORE_ONLINE:;
				this.setTitle("书架-在线图书");
				break;
			case  BOOK_SEARCH:	
				this.setTitle("图书-搜索");
				break;
			case  BOOK_READ_LOAD:
				this.setTitle("图书-阅读页加载");
				break;
			case  BOOK_DETAIL_LOAD:
				this.setTitle("图书-详情页加载");
				break;
			case  BOOK_CHAPTER_SWITCH:
				this.setTitle("图书-章节切换");
				break;
			default:					
				break;
		}
	},
	setTitle:function(title){
		var titleElement=document.getElementById("spanTitle");
		titleElement.innerHTML=title;		
	}
}


